package com.xiaoxi.exam.service;

import cn.xinzhili.xutils.core.FailureException;
import cn.xinzhili.xutils.core.http.Response;
import com.xiaoxi.exam.bean.enums.ExamType;
import com.xiaoxi.exam.bean.sync.OrderInfo;
import com.xiaoxi.exam.bean.sync.OrderInfoRequest;
import com.xiaoxi.exam.bean.sync.UserInfo;
import com.xiaoxi.exam.dao.AccountMapper;
import com.xiaoxi.exam.dao.PreOrderMapper;
import com.xiaoxi.exam.dao.ToeflOrderMapper;
import com.xiaoxi.exam.dao.UserMapper;
import com.xiaoxi.exam.model.Account;
import com.xiaoxi.exam.model.AccountExample;
import com.xiaoxi.exam.model.PreOrder;
import com.xiaoxi.exam.model.ToeflOrder;
import com.xiaoxi.exam.model.ToeflOrderExample;
import com.xiaoxi.exam.model.User;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

@Service
public class SyncService {

  private static HttpHeaders headers = new HttpHeaders();

  static {
    headers.setContentType(MediaType.APPLICATION_JSON);
  }


  @Autowired
  private PreOrderMapper preOrderMapper;
  @Autowired
  private ToeflOrderMapper toeflOrderMapper;
  @Autowired
  private UserMapper userMapper;
  @Autowired
  private AccountMapper accountMapper;
  @Autowired
  private RestTemplate restTemplate;

  public void syncOrder(long orderId) {
    PreOrder preOrder = preOrderMapper.selectByPrimaryKey(orderId);
    ToeflOrderExample example = new ToeflOrderExample();
    example.createCriteria().andOrderIdEqualTo(orderId).andDeletedAtIsNull();
    List<ToeflOrder> toeflOrders = toeflOrderMapper.selectByExample(example);
    List<OrderInfo> collect = toeflOrders.stream().map(to -> {
      OrderInfo orderInfo = new OrderInfo();
      orderInfo.setOrderId(orderId);
      orderInfo.setCity(to.getCity());
      orderInfo.setSchool(to.getSchool());
      orderInfo.setDetailId(to.getId());
      orderInfo.setUserId(preOrder.getUserId());
      orderInfo.setDate(to.getDate());
      orderInfo.setTel(to.getPhone());
      return orderInfo;
    }).collect(Collectors.toList());
    OrderInfoRequest request = new OrderInfoRequest();
    request.setOrderInfos(collect);
//    postSyncOrder(headers, request);
  }


  public Response syncUser(long userId) {

    User user = userMapper.selectByPrimaryKey(userId);
    if (Objects.nonNull(user)) {
      AccountExample accountExample = new AccountExample();
      accountExample.createCriteria().andUserIdEqualTo(userId).andTypeEqualTo(ExamType.TOEFL);
      accountExample.setOrderByClause("created_at desc");
      accountExample.setLimit(1);
      List<Account> accounts = accountMapper.selectByExample(accountExample);
      if (!accounts.isEmpty()) {
        Account account = accounts.get(0);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setTel(user.getTel());
        userInfo.setAccount(account.getAccount());
        userInfo.setPasswd(account.getPassword());
//        postSyncUser(headers, userInfo);
      }
    } else {
      UserInfo userInfo = new UserInfo();
      userInfo.setUserId(userId);
//      postSyncUser(headers, userInfo);
    }
    return Response.instanceSuccess();
  }

  public Response syncUser(UserInfo userInfo) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    postSyncUser(headers, userInfo);
    return Response.instanceSuccess();
  }

  @SuppressWarnings("unchecked")
  private void postSyncUser(HttpHeaders headers, UserInfo userInfo) {
    HttpEntity httpEntity = new HttpEntity(userInfo, headers);
    String s = restTemplate
        .postForObject("http://39.97.182.113:9099/sync/user", httpEntity, String.class);
    if (!StringUtils.isEmpty(s)) {
      throw new FailureException("user sync failed");
    }
  }

  @SuppressWarnings("unchecked")
  private void postSyncOrder(HttpHeaders headers, OrderInfoRequest orderInfoRequest) {
    HttpEntity httpEntity = new HttpEntity(orderInfoRequest, headers);
    String s = restTemplate
        .postForObject("http://39.97.182.113:9099/sync/order", httpEntity, String.class);
    if (!StringUtils.isEmpty(s)) {
      throw new FailureException("order sync failed");
    }
  }

  public void deleteOrder(long id) {
    restTemplate.delete("http://39.97.182.113:9099/sync/order/" + id);
  }
}
